package cn.mapway.plugin.rpc;

import cn.mapway.tools.rpc.GwtRpcGen;
import lombok.extern.slf4j.Slf4j;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.nutz.lang.Strings;

import java.util.ArrayList;
import java.util.List;

/**
 * 将API生成客户端
 *
 * @author zhangjianshe@gmail.com
 */
@Mojo(name = "rpc-gwt", defaultPhase = LifecyclePhase.GENERATE_SOURCES)
@Slf4j
public class RpcGwt extends AbstractMojo {
    /**
     * The path.
     */
    @Parameter(defaultValue = "${project.basedir}/src/main/java", property = "apiLocation", required = true)
    private String apiLocation;


    /**
     * 包含的表明，用逗号或者分号分隔
     */
    @Parameter(defaultValue = "", property = "includes", required = false)
    private String includes;

    /**
     * 排除的的表明，用逗号或者分号分隔
     */
    @Parameter(defaultValue = "", property = "excludes", required = false)
    private String excludes;

    /**
     * 包的名称
     */
    @Parameter(defaultValue = "test", property = "apiPackageName", required = true)
    private String apiPackageName;

    /**
     * 包的名称
     */
    @Parameter(defaultValue = "ApiClient", property = "apiClassName", required = true)
    private String apiClassName;

    /**
     * 作者
     */
    @Parameter(defaultValue = "", property = "author", required = true)
    private String author;

    /**
     * 作者
     */
    @Parameter(defaultValue = "${project.basedir}/src/main/java", property = "parseLocation", required = true)
    private String parseLocation;

    /**
     * 作者
     */
    @Parameter(defaultValue = "${project.basedir}/src/main/java", property = "parseLibLocation", required = true)
    private String parseLibLocation;

    /**
     * 作者
     */
    @Parameter(defaultValue = "${project.basedir}/src/main/java", property = "parsePackage", required = true)
    private String parsePackage;

    /**
     * 解析行
     *
     * @param data 数据
     * @return {@link List<String>}
     */
    private static List<String> parseLines(String data) {
        if (Strings.isBlank(data)) {
            return new ArrayList<String>();
        }
        data = Strings.trim(data);
        String[] items = Strings.split(data, false, ',', ';');
        ArrayList<String> list = new ArrayList<>(30);
        for (String item : items) {
            item = Strings.trim(item);
            if (!Strings.isBlank(item)) {
                list.add(item);
            }
        }
        return list;
    }

    /**
     * 执行
     *
     * @throws MojoExecutionException 魔力执行异常
     * @throws MojoFailureException   魔力衰竭例外
     */
    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {

        RpcCodeContext context = new RpcCodeContext();
        context.setExcludes(parseLines(excludes));
        context.setIncludes(parseLines(includes));
        context.setApiLocation(apiLocation);
        context.setApiPackageName(apiPackageName);
        context.setApiClassName(apiClassName);
        context.setAuthor(author);
        context.setParseLocations(parseLines(parseLocation));
        context.setParseLibLocations(parseLines(parseLibLocation));
        context.setParsePackages(parseLines(parsePackage));

        GwtRpcGen gen = new GwtRpcGen(context);
        gen.run();
    }
}
